<template>
  <div id="globalHeader">
    <a-row :wrap="false">
      <a-col flex="200px">
        <div class="title-bar">
          <img class="logo" src="../assets/rabbit.svg" alt="logo" />
          <div class="title">笨蛋数据库</div>
        </div>
      </a-col>
      <a-col flex="auto">
        <a-menu
          v-model:selectedKeys="current"
          mode="horizontal"
          :items="items"
          @click="doMenuClick"
          style="
            background-color: transparent;
            border: none;
            margin-top: 5px;
            font-weight: bold;
            color: rgba(255, 255, 255, 0.8);
          "
        />
      </a-col>
      <a-col flex="80px">
        <div class="user-login-status">
          <div v-if="loginUserStore.loginUser.id">
            {{ loginUserStore.loginUser.username ?? "无名者" }}
          </div>
          <div v-else>
            <a-button type="primary" href="/user/login" class="login-btn"
              >登录
            </a-button>
          </div>
        </div>
      </a-col>
    </a-row>
  </div>
</template>
<script lang="ts" setup>
import { h, ref } from "vue";
import { MailOutlined } from "@ant-design/icons-vue";
import type { MenuProps } from "ant-design-vue";
import { useRouter } from "vue-router";
import { useLoginUserStore } from "../store/useLoginUserStore";

const loginUserStore = useLoginUserStore();
const router = useRouter();

// 点击菜单后的路由跳转实现
const doMenuClick = (key: object) => {
  // 这里的key是个object,里面的key值才是需要的路由路径
  router.push({
    path: key.key,
  });
};

const current = ref<string[]>(["mail"]);

// 路由切换后更新当前选中菜单
router.afterEach((to, from, failure) => {
  current.value = [to.path];
});

const items = ref<MenuProps["items"]>([
  {
    key: "/",
    icon: () => h(MailOutlined),
    label: "主页",
    title: "主页",
  },
  {
    key: "/user/login",
    label: "用户登录",
    title: "用户登录",
  },
  {
    key: "/user/register",
    label: "用户注册",
    title: "用户注册",
  },
  {
    key: "others",
    label: h(
      "a",
      { href: "https://www.baidu.com", target: "_blank" },
      "这真的是百度"
    ),
    title: "这真的是百度",
  },
]);
</script>
<style scoped>
#globalHeader {
  position: fixed; /* 固定定位 */
  top: 0;
  left: 0;
  right: 0;
  z-index: 1000; /* 确保在轮播图上方 */
  height: 64px;
  background: rgba(128, 128, 128, 0.7);
  //backdrop-filter: blur(5px);
  //background-color: rgba(255, 255, 0, 0.2);
  //height: 64px;
  //display: flex;
}

.title-bar {
  display: flex;
  align-items: center;
}

.title-bar img {
  margin-left: 20px;
  margin-top: 5px;
}

.title {
  color: black;
  font-size: 18px;
  margin-left: 18px;
}

.logo {
  height: 48px;
}

.user-login-status {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  margin-top: 20px;
  margin-right: 20px;
  color: rgba(255, 255, 255, 0.8);
  font-weight: bold;
}

.title {
  font-size: 20px;
  font-weight: bold;
  color: rgba(255, 255, 255, 0.8);
}
</style>
